package com.woniuxy.yygh.hsop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yygh.common.exception.YyghException;
import com.woniuxy.yygh.common.result.Result;
import com.woniuxy.yygh.common.utils.MD5;
import com.woniuxy.yygh.hsop.service.HospitalSetService;
import com.woniuxy.yygh.model.hosp.HospitalSet;
import com.woniuxy.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 医院设置controller hospitalSet——mysql表
 * <P> Date: 2022/2/27:13:53
 * <P> Description:
 */
@Api("医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    //注入医院实现类
    @Autowired
    private HospitalSetService hospitalSetService;

    //测试 查询医院设置表所有信息
    @ApiOperation(value = "获取医院设置所有信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet() {

        return Result.ok(hospitalSetService.list());
    }

    /**
        * TODO:根据医院设置表的的id逻辑删除医院
    **/
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        if (hospitalSetService.removeById(id)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    /**
        * TODO:对医院设置表分页 可以根据医院名称和编号来分页
    **/
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象  传递当前页 页大小
        Page<HospitalSet> page = new Page<>(current, limit);

        //构建查询条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //医院名称和编号
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }

        //调用条件查询和分页
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        return Result.ok(pageHospitalSet);
    }

    /**
        * TODO: 新添加医院设置 前端返回对应对象即可
    **/
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet) {

        //设置状态 1可用 0不能使用
        hospitalSet.setStatus(1);

        //生成随机的签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    /**
        * TODO:根据医院设置id获取医院设置指定的设置
    **/
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHosSet(@PathVariable Long id){
        return Result.ok(hospitalSetService.getById(id));
    }

    /**
        * TODO:根据医院设置id修改医院设置
    **/
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    /**
        * TODO:根据医院设置ids 逻辑删除医院设置
    **/
    @ApiOperation(value = "批量删除医院设置")
    @PostMapping("/batchRemoveHospitalSet")
    public Result batchRemoveHospitalSet(@RequestBody List<Integer> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
        * TODO:根据医院设置id 对医院设置状态的改变
    **/
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping ("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet == null){
            return Result.fail();
        }

        //设置状态
        hospitalSet.setStatus(status);

        hospitalSetService.updateById(hospitalSet);

        return Result.ok();
    }


    /**
        * TODO: 根据医院设置id 返回签名密钥
    **/
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet == null){
            return Result.fail();
        }else {
            return Result.ok(hospitalSet.getSignKey());
        }

    }

}
